﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeEdu.MODEL;
using LeEdu.BLL;
using System.Net;

namespace LeEdu.Web.Controllers
{
    public class LoginController : Controller
    {


        /// <summary>
        /// 实例化UserInfoBLL
        /// </summary>
        UserInfoBLL userbll = new UserInfoBLL();

        /// <summary>
        /// 实例化LoginDailyBLL
        /// </summary>
        LoginDailyBLL bll = new LoginDailyBLL();

        /// <summary>
        /// 显示登陆页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 登陆方法
        /// </summary>
        /// <param name="Email">用户邮箱</param>
        /// <param name="Upwd">用户密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LoginShow(string Email, string Upwd)
        {
            UserInfo user = new UserInfo();
            user = userbll.login(Email,Upwd);
            if (user != null)
            {
                if (user.SealState == 1)
                {
                    ViewBag.str = "您的账户已经被封，请联系管理员";
                    return View("Index");
                }
                else
                {
                    if (user != null && user.TeacherCheck == 0)
                    {
                        return Content("<script>alert('您没有获取教师资格,请联系管理员后登陆！！！');location.href='/Login/Index'</script>");
                    }
                    else
                    {
                        Session["id"] = user.UserInfoId;
                        Session["User"] = userbll.login(Email, Upwd);
                        Session["Uname"] = userbll.login(Email, Upwd).Uname;
                        Session["RoleId"] = user.RoleId;
                        int id = user.UserInfoId;//用户id
                        DateTime time = DateTime.Now;//登录时间

                        //string strHostName = Dns.GetHostName(); //得到本机的主机名
                        //IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //
                        //string IP = ipEntry.AddressList[1].ToString();//获得IP

                        string Ip = string.Empty;
                        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                        {
                            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                            {
                                Ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                            }
                            else
                            {
                                Ip = System.Web.HttpContext.Current.Request.UserHostAddress;
                            }
                        }
                        else
                        {
                            Ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                        }

                        LoginDaily ld = new LoginDaily();
                        ld.LoginTime = time;
                        ld.UserId = id;
                        ld.IP = Ip;
                        bll.AddEntity(ld);
                        return Content("<script>location.href='/Home/Index'</script>");
                    }
                }
            }
            else
            {
                return Content("<script>alert('登录失败，密码或用户名错误，请重新登录！');location.href='/Login/Index'</script>");
            }
        }
    }
}
